package com.xfcycle.zebrabikegp.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.assist.AlarmAssist;
import com.xfcycle.zebrabikegp.bean.AlarmInfoResult;
import com.xfcycle.zebrabikegp.constant.AlarmConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AlarmListActivity extends RootActivity {

    private ImageView iv_about_back;
    private ListView lv_alarm_info;

    private AlarmAssist alarmAssist;
    private List<AlarmInfoResult> alarmInfoList;
    private AlarmInfoAdapter alarmInfoAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_list);

        initView();
        initViewListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setViewContent();
    }

    private void initView() {
        iv_about_back = (ImageView) findViewById(R.id.iv_about_back);
        lv_alarm_info = (ListView) findViewById(R.id.lv_alarm_info);

        alarmAssist = AlarmAssist.getInstance();

        alarmInfoList = new ArrayList<>();
        Map<String, AlarmInfoResult> alarmInfoMap = alarmAssist.getAlarmInfoMap();
        for (AlarmInfoResult alarmInfo : alarmInfoMap.values()) {
            alarmInfoList.add(alarmInfo);
        }

        //绑定报警信息列表数据源
        alarmInfoAdapter = new AlarmInfoAdapter();
        lv_alarm_info.setAdapter(alarmInfoAdapter);
    }

    private void initViewListener() {
        iv_about_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });
    }

    private void setViewContent() {
        alarmInfoList.clear();
        Map<String, AlarmInfoResult> alarmInfoMap = alarmAssist.getAlarmInfoMap();
        for (AlarmInfoResult alarmInfo : alarmInfoMap.values()) {
            alarmInfoList.add(alarmInfo);
        }
        alarmInfoAdapter.notifyDataSetChanged();
    }

    /***********************************************************************************************/
    /**                                       自定义BaseAdapter方法                                **/
    /***********************************************************************************************/

    /**
     * 自定义缓存复用类
     */
    class ViewHolder {
        TextView tv_alarm_name;
        TextView tv_alarm_type;
    }

    /**
     * 自定义BaseAdapter
     */
    class AlarmInfoAdapter extends BaseAdapter {

        private ViewHolder viewHolder;

        @Override
        public int getCount() {
            return alarmInfoList.size();
        }

        @Override
        public Object getItem(int position) {
            return alarmInfoList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return alarmInfoList.get(position).hashCode();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(AlarmListActivity.this);
            final AlarmInfoResult alarmInfoResult = alarmInfoList.get(position);

            if (null == convertView) {
                convertView = inflater.inflate(R.layout.item_alarm_info_list, null);
                viewHolder = new ViewHolder();
                viewHolder.tv_alarm_name = (TextView) convertView.findViewById(R.id.tv_alarm_name);
                viewHolder.tv_alarm_type = (TextView) convertView.findViewById(R.id.tv_alarm_type);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }


            String bikeNickName = alarmInfoResult.getBikeNickName();
            viewHolder.tv_alarm_name.setText((null == bikeNickName || "".equals(bikeNickName) ? alarmInfoResult.getBikeName() : bikeNickName));

            switch (alarmInfoResult.getAlarmType()) {
                case AlarmConstant.ALARM_TYPE_STOLEN:
                    viewHolder.tv_alarm_type.setText("Stolen");
                    break;
                case AlarmConstant.ALARM_TYPE_LOW_POWER:
                    viewHolder.tv_alarm_type.setText("Low power");
                    break;
                case AlarmConstant.ALARM_TYPE_MAIN_POWER_OFF:
                    viewHolder.tv_alarm_type.setText("Main power off");
                    break;
                case AlarmConstant.ALARM_TYPE_UMOUNT_485:
                    viewHolder.tv_alarm_type.setText("Not mount 485");
                    break;
            }

            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (AlarmConstant.ALARM_TYPE_STOLEN.equals(alarmInfoResult.getAlarmType())) {
                        //车辆被偷 进入定位界面
                        Map<String, String> params = new HashMap<>();
                        params.put("bikeName", alarmInfoResult.getBikeName());
                        activityChange(ALARM_MAP_ACTIVITY, params, null);

                    } else {
                        //其他报警 跳出展示
                    }
                }
            });

            return convertView;
        }
    }
}
